//
//  LanguageTool.swift
//  StoreProject
//
//  Created by CYC on 1998/2/27.
//


import Foundation


class LanguageTool {
    
    static let shared = LanguageTool()
    
    let defaults = UserDefaults.standard
    let lKEY = "LanguageTool"
    
    func valueWithKey(key: String) -> String {
        
        let bundle = Bundle(path: Bundle.main.path(forResource:self.getLanguage() , ofType: "lproj")!)!
        return NSLocalizedString(key, tableName: "LanguageFile", bundle: bundle, value: "", comment: "")
    }
    
    func setLanguage(language: String) {
        
        defaults.set(language, forKey: lKEY)
        defaults.synchronize()
    }
    
    func getLanguage() -> String {
        var str = ""
        if let language = defaults.value(forKey: lKEY) as? String {
            str = language
        } else {
            str = getSystemLanguage()
        }
        return str
    }
    
    private func getSystemLanguage() -> String {
        return "en"
    }
}

extension String {
    var languageText: String {
        return LanguageTool.shared.valueWithKey(key: self)
    }
}
